package cn.remex.wechat.utils;

import cn.remex.core.util.JsonHelper;
import cn.remex.core.util.Judgment;
import cn.remex.wechat.utils.coordinate.Gps;
import cn.remex.wechat.utils.coordinate.PositionUtil;
import com.fasterxml.jackson.databind.JsonNode;

import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by xuyanhua on 2017/4/20.
 */
public class LocationUtils {


    public static String dealLatitudeAndLongitudeGetAddr(String latitude,String longitude){
        String address = "";
        if(Judgment.notEmpty(latitude)&&Judgment.notEmpty(longitude)){
            //精度取到小数点后两位
            if(latitude.indexOf(".")>-1 && (latitude.length()-1)>(latitude.indexOf(".")+2)
                    && longitude.indexOf(".")>-1 && (longitude.length()-1)>(longitude.indexOf(".")+2)){
                latitude = latitude.substring(0,latitude.indexOf(".")+3);
                longitude = longitude.substring(0,longitude.indexOf(".")+3);
                address = getAddrByLongitudeAndLatitude(longitude,latitude);
            }
        }
        return address;
    }



    public static String getAddrByLongitudeAndLatitude(String longitude, String latitude){
        String address = getAddrByLongitudeAndLatitudeAliyun(longitude,latitude);
        JsonNode jsonNode = JsonHelper.toJsonNode(address);

        JsonNode addrList = jsonNode.get("addrList");
        JsonNode poi = addrList.get(0);
        String name = poi.get("name").textValue();
        String admName = poi.get("admName").textValue();
        //处理直辖市问题
        if(Judgment.notEmpty(admName)){
            String[] admNameArr = admName.split(",");
            if(admNameArr.length>2 && admNameArr[0].equals(admNameArr[1])){
                admName = admName.substring(admName.indexOf(",")+1,admName.length());
            }
        }

        System.out.println(admName+name);
        return admName+name;
    }


    public static String getAddrByLongitudeAndLatitudeAliyun(String longitude, String latitude){
        //latitude 纬度 小  longitude 经度  大
        //参数解释: 纬度,经度 type 001 (100代表道路，010代表POI，001代表门址，111可以同时显示前三项)
        String urlString = "http://gc.ditu.aliyun.com/regeocoding?l="+latitude+","+longitude+"&type=010";
        String res = "";
        try {
            URL url = new URL(urlString);
            HttpURLConnection conn = (java.net.HttpURLConnection)url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(),"UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                res += line+"\n";
            }
            in.close();
        } catch (Exception e) {
            System.out.println("error in wapaction,and e is " + e.getMessage());
        }
        return res;
    }


    public static void main(String[] args) {
        // lat 39.97646
        //log 116.3039
//        "114.30", "30.54"
        //116.3354 39.8877

       Gps gps= PositionUtil.gps84_To_Gcj02(39.8877,116.3354);

        String address = getAddrByLongitudeAndLatitude(String.valueOf(gps.getWgLon()), String.valueOf(gps.getWgLat()));
//        String address = getAddrByLongitudeAndLatitude("116.3039", "39.97646");
        System.out.println(address);

    }
}
